package com.pxx.file.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.pxx.file.config.OssConfig;
import com.pxx.file.service.PictureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.UUID;
import java.util.Date;

/**
 * @author : Yoo19
 * @description :
 * @createDate : 2025/6/24 16:16 16
 */
@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private OssConfig ossConfig;

    //规定一下图片的格式
    public static final String[] CONTENT_TYPES = new String[]{
            "bmp",
            "gif",
            "jpeg",
            "png",
            "jpg",
    };

    @Autowired
    private OSSClient ossClient;


    @Override
    public String uploadImage(MultipartFile file) {
        String return_url = "";

        //效验的标识
        Boolean flag = false;
        //效验图片格式
        //首先编译图片的格式数组，获取当前文件的格式 然后进行对比，如果比对中的话，则直接跳出循环，继续
        for (String contentType : CONTENT_TYPES) {
            if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(),contentType)){
                flag = true;
                break;
            }
        }

        //如果格式不对，则直接返回null
        if (!flag){
            return return_url;
        }


        //获取文件的原名称
        String originalFileName  =  file.getOriginalFilename();
        //获取文件类型 .jpg
        String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));
        //构建上传的路径   2025/06/24
        String datePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        //生成的文件名称   images/2025/06/24/123456789.jpg
        String newFileNameUrl=  ossConfig.getFileHost()+"/"+datePath+"/"+
                UUID.randomUUID().toString().replace("-","")+fileType ;

        //上传
        try {
            InputStream inputStream = file.getInputStream();
            ObjectMetadata objectMetadata = new ObjectMetadata();
            //声明文件的类型是什么样子的
            objectMetadata.setContentType("image/jpg");
            //上传
            ossClient.putObject(ossConfig.getBucketName(),newFileNameUrl,inputStream,objectMetadata);

            return "http://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+newFileNameUrl;


        } catch (IOException e) {
            e.printStackTrace();
        }

        return return_url;
    }

    @Override
    public void deleteImage(String url) {
        OSSClient ossClient = null;
        try {
            //阿里云建立连接
            ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccesskeyId(), ossConfig.getAccesskeySecret());
            //根据bucketName的名称
            String bucketName = ossConfig.getBucketName();
            //截取路径为images的开头
            int lastIndexOf = url.lastIndexOf("/bwie-pay");
            //截取文件名称
            String newPath = url.substring(lastIndexOf + 1);
            //删除
            ossClient.deleteObject(bucketName,newPath);
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            //关闭链接
            ossClient.shutdown();
        }
    }
}
